package com.dameng.jianyouquan.mvp.model;

import android.text.TextUtils;

import com.dameng.jianyouquan.bean.SendVerificationBean;
import com.dameng.jianyouquan.api.NetWorkManager;
import com.dameng.jianyouquan.utils.EdittextUtils;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RegisterModel {

    public void sendVerificationCode(String phoneNum, OnSendVerificationCodeListener listener) {

        if (TextUtils.isEmpty(phoneNum)) {
            listener.sendVerificationCodeFail("手机号不能为空");
        }else if (!EdittextUtils.isMobileNO(phoneNum)){
            listener.sendVerificationCodeFail("请输入正确的手机号");
        } else {

            NetWorkManager.getService()
                    .getVerificationCode(phoneNum)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<ResponseBody>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(ResponseBody responseBody) {
                            String response="";
                            try {
                                response= responseBody.string();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            Gson gson = new Gson();
                            SendVerificationBean sendVerificationBean = gson.fromJson(response, SendVerificationBean.class);
                            if (sendVerificationBean.getCode()==200){
                                listener.sendVerificationCodeSuccess();
                            }else {
                                listener.sendVerificationCodeFail(sendVerificationBean.getMsg()+"");
                            }
                        }
                    });

        }
    }


    public void register(String phoneNum, String verificationCode, String pwd, OnRegisterListener listener) {
        if (TextUtils.isEmpty(phoneNum) || TextUtils.isEmpty(verificationCode) || TextUtils.isEmpty(pwd)) {
            listener.registerFail("手机号或者验证码或者密码不能为空");
        }else if (!EdittextUtils.isMobileNO(phoneNum)){
            listener.registerFail("请输入正确的手机号");
        }else if (EdittextUtils.isPWD(pwd)){
            listener.registerFail("密码格式不正确");
        } else {
            NetWorkManager.getService()
                    .getRegister(phoneNum,pwd,verificationCode)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(responseBody -> {

                    });

        }
    }


    public interface OnRegisterListener {

        void registerSuccess();

        void registerFail(String msg);
    }


    public interface OnSendVerificationCodeListener {

        void sendVerificationCodeSuccess();

        void sendVerificationCodeFail(String msg);
    }


}
